package com.hcl.linkedlist;

public class DoubleLinkedListDemo {
    public static void main(String[] args) {
        HeroNode2 heroNode1 = new HeroNode2(4, "张杰1", "JsonZhang1");
        HeroNode2 heroNode = new HeroNode2(1, "张杰2", "JsonZhang2");
        HeroNode2 heroNode2 = new HeroNode2(3, "鹿晗", "luhan");
        DoubleLinkedList doubleLinkedList = new DoubleLinkedList();
        doubleLinkedList.add(heroNode);
        doubleLinkedList.add(heroNode1);
        doubleLinkedList.add(heroNode2);
//        doubleLinkedList.list();
//        HeroNode2 heroNode3 = new HeroNode2(1, "鹿晗1", "luhan1");
//        doubleLinkedList.update(heroNode3);
//        doubleLinkedList.list();
//        System.out.println("删除");
//        doubleLinkedList.delete(1);
//        doubleLinkedList.list();

    }
}

class DoubleLinkedList {
    private HeroNode2 head = new HeroNode2(0,"","");

    public HeroNode2 getHead() {
        return head;
    }


    public void delete(int no){
        if (head.next == null){
            System.out.println("链表空");
            return;
        }
        HeroNode2 temp = head.next;
        boolean flag = false;
        while (true){
            if (temp == null){
                break;
            }
            if (temp.no == no){
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag){
            temp.pre.next = temp.next;
            // 最后一个节点的next为null
            if (temp.next != null){
                temp.next.pre = temp.pre;
            }
        } else {
            System.out.println("要删除的数据不存在");
        }
    }

    public void update(HeroNode2 heroNode){
        if (head.next==null){
            System.out.println("链表空");
            return;
        }
        // 头节点不动，需要一个temp辅助遍历
        HeroNode2 temp = head.next;
        boolean flag = false;
        while (true){
            // 遍历到了链表最后
            if (temp == null){
                break;
            }
            if (temp.no == heroNode.no){
                flag = true;
                break;
            }
            // temp后移
            temp = temp.next;
        }
        if (flag){
            temp.name = heroNode.name;
            temp.nickname = heroNode.nickname;
        } else {
            System.out.println("没有找到编号为" + heroNode.no + "的数据");
        }
    }

    public void add(HeroNode2 heroNode){
        HeroNode2 temp = head;
        while (true){
            // 已经遍历完了
            if (temp.next == null){
                break;
            }
            temp = temp.next;
        }
        temp.next = heroNode;
        heroNode.pre = temp;
    }

    public void list(){
        if (head.next == null){
            System.out.println("链表数据为空");
            return;
        }
        HeroNode2 temp = head.next;
        while (temp != null){
            System.out.println(temp);
            temp = temp.next;
        }
    }
}
class HeroNode2 {
    public int no;
    public String name;
    public String nickname;
    public HeroNode2 next;  // 下一个节点
    public HeroNode2 pre;   // 上一个节点

    public HeroNode2(int no, String name, String nickname) {
        this.no = no;
        this.name = name;
        this.nickname = nickname;
    }

    @Override
    public String toString() {
        return "HeroNode2{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickname='" + nickname + '\'' +
                '}';
    }
}
